package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcDaysTimeConfig;
import com.csun.cmny.provider.model.dto.DaysTimeConfigDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcDaysTimeConfigService;
import com.csun.cmny.provider.validator.DaysTimeConfigSaveValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/daysTimeConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcDaysTimeConfigController extends BaseController {

    @Resource
    private OmcDaysTimeConfigService omcDaysTimeConfigService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 保存
     * @param: [daysTimeConfigDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/save")
    public Wrapper saveDaysTimeConfig(@Validated(DaysTimeConfigSaveValidator.class) @RequestBody DaysTimeConfigDto daysTimeConfigDto,
                                      BindingResult br) {

        int result = omcDaysTimeConfigService.saveDaysTimeConfig(convert(daysTimeConfigDto),getLoginAuthDto());

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询
     * @param: [groupId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getByGroupId/{groupId}")
    public Wrapper saveDaysTimeConfig(@PathVariable Long groupId) {

        OmcDaysTimeConfig omcDaysTimeConfig = new OmcDaysTimeConfig();
        omcDaysTimeConfig.setGroupId(groupId);
        omcDaysTimeConfig = omcDaysTimeConfigService.selectOne(omcDaysTimeConfig);

//        return WrapMapper.ok(omcDaysTimeConfig);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), omcDaysTimeConfig);
    }

    /**
     * 转换
     * @param: [daysTimeConfigDto]
     * @return: com.csun.cmny.provider.model.domain.OmcDaysTimeConfig
     */
    public OmcDaysTimeConfig convert(DaysTimeConfigDto daysTimeConfigDto){
        OmcDaysTimeConfig omcDaysTimeConfig = new OmcDaysTimeConfig();
        BeanUtils.copyProperties(daysTimeConfigDto,omcDaysTimeConfig);
        return omcDaysTimeConfig;
    }
}
